<?php
// +----------------------------------------------------------------------
// | makePHP 
// +----------------------------------------------------------------------
// | Copyright (c) 2009 http://makephp.org All rights reserved.
// +----------------------------------------------------------------------
// | Licensed BSD.(http://www.make.org/BSD-license.txt)
// +----------------------------------------------------------------------
// | Author: 叁石(sanshi) <sanshi0815@tom.com>
// | Author: 白家昌<baijc2004cn@126.com>
// +----------------------------------------------------------------------
/**
 *  Entrance File of the Control class.
 *
 * @Author:    slash<phpunion@gmail.com>
 * @Package:   Interface of the View class. 
 * @Version:   $Id: info_other_user.class.php 2012-04-25 10:52:45 $
 * @Show Note: 相关人管理
 */
Class info_other_user extends TheAdmin
{
		/* DataBase object */
		public $db;
	
		/* View the data page limit items */
		public $pageSize = 10;
		
		/**
		 *	Construct this module class.
		*/
		public function info_other_user()
		{
		}
		public function set()
		{
			$this->TheAdmin();
			//定义模版
			$this->tplFile ="info_other_user.tpl";
			$this->admin_user_id = $this->userRequest->get('admin_user_id');
			//取得数据库连接
			$this->db = Factory::getDB("info_other_user");
			//取得用户对于该类的权限
			$user_p = $this->get_user_popedom_by_class_name(__CLASS__);
			$class_p_arr = $this->show_p();
			//取得该类的权值数组
			$class_method = $class_p_arr['class_method'];
			//取得用户操作菜单
			$menu = array();
			foreach ($class_method as $k=>$v)
			{
				if( (parse_power($user_p,$v[2]) && (isset($v[3]) ?$v[3] :false)) || IS_ROOT)
				{
					array_push($menu,array('url'=>"?c=".__CLASS__."&m={$v[0]}",'title'=>$v[1]));
				}
			}
			//用户有权菜单生成 可以添加其他操作
			$this->smarty->assign('menus',$menu);
			$this->smarty->assign("main_menus",$this->get_user_menus());
		}
	
		/**
		 *	Call class construct method.
		*/
		public function __construct()
		{
				$this->info_other_user();
		}
		public function _display()
		{
			$this->smarty->display($this->tplFile);
		}
		/**
		 *	default method.
		*/
		public function show()
		{
			$type = isset($_GET['type']) ? intval($_GET['type']) : 0; //来源：1 客户 2 员工
			$obj_id = isset($_GET['obj_id']) ? intval($_GET['obj_id']) : 0; //客户ID或员工ID
			if($type == 1) {
				$where = "guest_id='{$obj_id}'";
				$db_obj = Factory::getDB("v_guest_other");
			} else if($type == 2) {
				$where = "admin_user_id='{$obj_id}'";
				$db_obj = Factory::getDB("v_user_other");
			}else {
				$where = 1;
				$db_obj = $this->db;
			}

				//$where = 1;
				$col = "other_id";				
				$rowNo = $db_obj->getSumNumber($db_obj->tableName,$where);
				//数据库执行sql语句出错
				//取得错误的sql语句
				//$error_sql = end($db_obj->getDebugMsg());
				//echo $error_sql;
				if( 0 > $rowNo) goToIndex($rowNo);
				//分页
				$page = Factory::getBaseClass('pagination');
				$page->set($rowNo,$this->pageSize);
				$numrows = $this->pageSize;
				$offset  = $page->getSqlMove();
				$arr = $db_obj->getAll($where,$col,$numrows,$offset);
				admin_log($this->admin_user_id,__METHOD__,'查看',1);
				//数据库执行select语句出错
				//取得错误的sql语句
				//$error_sql = end($db_obj->getDebugMsg());
				//echo $error_sql;
				if(0 > $arr ) goToIndex($arr);
				
				$this->smarty->assign('type',$type);
				$this->smarty->assign('obj_id',$obj_id);

				$this->smarty->assign('arr',$arr);
				//默认方法
				$this->smarty->assign("page_footer",$page->getFooter().$page->getJumpPage());
				$this->smarty->assign('act','list');
				$this->smarty->display($this->tplFile);
		}
		
		/**
		 *	add method.
		*/
		public function add()
		{
			$type = isset($_GET['type']) ? intval($_GET['type']) : 0; //来源：1 客户 2 员工
			$obj_id = isset($_GET['obj_id']) ? intval($_GET['obj_id']) : 0;

			if(!in_array($type, array(1,2))) {
				alertMsg2("非法操作", 'index.php');
			}

			if($type == 1){				
				if (empty($obj_id)) {
					alertMsg2("请先添加客户", 'index.php?c=info_guest');
				}
			}
			if($type == 2){				
				if (empty($obj_id)) {
					alertMsg2("请先添加员工", 'index.php?c=admin_user');
				}
			}

				//添加数据
				if(!isset($_REQUEST['post']) || trim($_REQUEST['post'])!=1)
				{
						$this->smarty->assign('type', $type);
						$this->smarty->assign('obj_id', $obj_id);

						//使用模版，展示提交界面
						$this->smarty->assign('act_title','添加');
						$this->smarty->assign('act','add');
						$this->smarty->display($this->tplFile);
				} else {
						//用户提交，把数据插入数据库
						$temp = $this->getValue();
						$insrt = $this->db->insert_info_other_user($temp);
						$error_no = 0 > $insrt ?  $insrt :'-1501';
						admin_log($this->admin_user_id,__METHOD__,"id={$insrt}",2);
						//取得错误的sql语句
						//$error_sql = end($this->db->getDebugMsg());
						//echo $error_sql;

						/** 添加关系表 **/
						$other_type = isset($_POST['other_type']) ? trim($_POST['other_type']) : ''; //添加关系类别
						$this->setOther($type, $insrt, $obj_id, $other_type);

						alertMsg($error_no, "index.php?c=info_other_user&obj_id={$obj_id}&type={$type}");
				}
		}
		
		/**
		 *	edit method.
		*/
		public function edit()
		{
			$type = isset($_GET['type']) ? intval($_GET['type']) : 0; //来源：1 客户 2 员工
			$obj_id = isset($_GET['obj_id']) ? intval($_GET['obj_id']) : 0;
				//取得id，也就是唯一索引
				$other_id = isset($_REQUEST['other_id']) ? intval(trim($_REQUEST['other_id'])) : 0 ;
				//取得是否查看详情
				$is_show = isset($_REQUEST['is_show']) ? intval(trim($_REQUEST['is_show'])) : 0 ;

				//关系类别
				$other_type_tab = $this->getOtherType($other_id, $type);

				if(!isset($_REQUEST['post']) || trim($_REQUEST['post'])!=1)
				{
						//使用模版，展示提交界面
						$result = $this->db->getOne($other_id);
						if(empty($result))
						{
							alertMsg('-2001', 'index.php?c=info_other_user');
						}
						foreach ($result AS $k=>$v)
						{
							$this->smarty->assign("{$k}",$v);
						}

						$this->smarty->assign('type', $type);
						$this->smarty->assign('obj_id', $obj_id);
						$this->smarty->assign('other_type', $other_type_tab);

						$this->smarty->assign('is_show',$is_show);
						$this->smarty->assign('act_title','修改');
						$this->smarty->assign('act','edit');
						$this->smarty->display($this->tplFile);
				} else {
						//用户提交，把数据插入数据库
						//设置where条件
						$where = "other_id=$other_id";
						$temp = $this->getValue(1);
						$update = $this->db->update_info_other_user($temp,$where);
						admin_log($this->admin_user_id,__METHOD__,$where,2);
						$error_no = 0 > $update ?  $update :'-1502';
						//取得错误的sql语句
						//$error_sql = end($this->db->getDebugMsg());
						//echo $error_sql;

						$other_type = isset($_POST['other_type']) ? trim($_POST['other_type']) : ''; //添加关系类别
						if($update>=0 && $other_type_tab != $other_type) { //update
							$this->updateOther($type, $other_id, $other_type);
						}
						alertMsg($error_no, "index.php?c=info_other_user&obj_id={$obj_id}&type={$type}");
				}
		}
		
		/**
		 *	del method.
		*/
		public function del()
		{
			$type = isset($_GET['type']) ? intval($_GET['type']) : 0; //来源：1 客户 2 员工
				//取得id，也就是唯一索引
				$other_id = isset($_REQUEST['other_id']) ? intval(trim($_REQUEST['other_id'])) : 0 ;
				$where = "other_id=$other_id";
				$del=$this->db->delete_info_other_user($where);
				admin_log($this->admin_user_id,__METHOD__,$where,3);
				$error_no = 0 > $del ?  $del :'-1503';
				//取得错误的sql语句
				//$error_sql = end($this->db->getDebugMsg());
				//echo $error_sql;

				if($del > 0) {
					$this->delOther($type, $other_id);
				}
				alertMsg($error_no, 'index.php?c=info_other_user');
		}
		
		/**
		 *	get request method.
		*/
		public function &getValue($isEdit=0)
		{
			$temp = array();
			$check = Factory::getBaseClass("checkBase");
			if($isEdit){
				$temp['other_id'] = $_POST['other_id'];
			} else {
				$temp['other_id'] = "";
			}
			//补充其他的字段
			$temp['other_name'] = $_POST['other_name'];
			$temp['ohter_en_name'] = $_POST['ohter_en_name'];
			$temp['other_sex'] = $_POST['other_sex'];
			$temp['other_worker'] = $_POST['other_worker'];
			$temp['other_company'] = $_POST['other_company'];
			$temp['other_address'] = $_POST['other_address'];
			$temp['other_mob'] = $_POST['other_mob'];
			$temp['other_tel'] = $_POST['other_tel'];
			$temp['other_fox'] = $_POST['other_fox'];
			$temp['other_mail'] = $_POST['other_mail'];
			$temp['other_qq'] = $_POST['other_qq'];
			$temp['other_msn'] = $_POST['other_msn'];
			$temp['other_sns'] = $_POST['other_sns'];
			$temp['other_birthday'] = $_POST['other_birthday'];
			$temp['other_marry'] = $_POST['other_marry'];
			$temp['other_sfz'] = $_POST['other_sfz'];
			$temp['other_home'] = $_POST['other_home'];
			$temp['other_user_zip'] = $_POST['other_user_zip'];
			$temp['other_home_tel'] = $_POST['other_home_tel'];
			$temp['other_work_info'] = $_POST['other_work_info'];
			$temp['other_home_info'] = $_POST['other_home_info'];
			$temp['other_intree_info'] = $_POST['other_intree_info'];
			$temp['other_studio_info'] = $_POST['other_studio_info'];
			$temp['other_info'] = $_POST['other_info'];
			$temp['laster'] = $this->admin_user_id;
			$temp['update_time'] = time();
			admin_log($this->admin_user_id,__METHOD__,serialize($temp),2);	
			return $temp;
		}
		/**
		 *	get class method and popedom.
		*/
		public function show_p()
		{
			//该方法为，提供用户使用方法定义，非web用户权限相关不用写
			return array(
				/* 定义该类中文名称*/
				//'class_name'=>array(__CLASS__,'相关人管理'),
				'class_method' =>array(
					//array（方法名，中文解释，权值,是否为菜单）
					'show'  => array('show','相关人列表',1,false),
					'add'  => array('add','相关人添加',2,false),
					'edit'  => array('edit','相关人编辑',4,false),
					'del'  => array('del','相关人删除',8,false),
					'show_p'=> array('show_p','',0,false)
				)
			);
		}

		/**
		 * (功能描述) 客户或员工的相关人表对应关系表
		 *
		 * @param int type 相关来源：1 客户 2 员工
		 * @param int other_id 相关ID
		 * @param int obj_id 来源对象ID: 客户ID或员工ID
		 * @param int other_type 关系类别(添加时参数带入)
		 * @return
		 */
		function setOther($type, $other_id, $obj_id, $other_type)
		{
			if($type == 1) {
				$db_igo = Factory::getDB('info_guest_other');
				$values = array(
					'other_id' => $other_id,
					'guest_id' => $obj_id,
					'guest_other_type' => $other_type
				);
				$db_igo->insert_info_guest_other($values);
			} else if($type == 2) {
				$db_iuo = Factory::getDB('info_user_other');
				$values = array(
					'other_id' => $other_id,
					'admin_user_id' => $obj_id,
					'user_other_type' => $other_type
				);
				$db_iuo->insert_info_user_other($values);			    
			}else {}
		}

		/** 获取关系类别 **/
		function getOtherType($other_id='', $type=1)
		{
			$other_type = '';

			if($type == 1) {
				$db_igo = Factory::getDB('info_guest_other');
				$res = $db_igo->getOne($other_id);
				$other_type = isset($res['guest_other_type']) ? trim($res['guest_other_type']) : '';
			} else if($type == 2) {
				$db_iuo = Factory::getDB('info_user_other');
				$res = $db_iuo->getOther($other_id);
				$other_type = isset($res['user_other_type']) ? trim($res['user_other_type']) : '';	    
			}else {}

			return $other_type;
		}

		function updateOther($type, $other_id, $other_type)
		{
			$where = "other_id='{$other_id}'";
			if($type == 1) {
				$db_igo = Factory::getDB('info_guest_other');
				$values = array(
					'guest_other_type' => $other_type
				);				
				$db_igo->update_info_guest_other($values, $where);
			} else if($type == 2) {
				$db_iuo = Factory::getDB('info_user_other');
				$values = array(
					'user_other_type' => $other_type
				);
				$db_iuo->update_info_user_other($values, $where);
			}else {}
		}

		function delOther($type, $other_id)
		{
			$where = "other_id='{$other_id}'";
			if($type == 1) {
				$db_igo = Factory::getDB('info_guest_other');
				$db_igo->delete_info_guest_other($where);
			} else if($type == 2) {
				$db_iuo = Factory::getDB('info_user_other');
				$db_iuo->delete_info_user_other($where);
			}else {}
		}
}

?>